<?php


namespace Tbf\Component\Image;


class FontDraw {
    /**
     * @var float
     */
    protected $size = 12.0;
    /**
     * @var float
     */
    protected $angle = 0.0;
    /**
     * @var string
     */
    protected $fontfile;
    /**
     * @var string
     */
    protected $text;
    /**
     * upper left corner, X position
     * @var float
     */
    protected $x = 0.0;
    /**
     * upper left corner, Y position
     * @var float
     */
    protected $y = 0.0;
    /**
     * @var int
     */
    protected $color_index = 0;

    /**
     * @param float $angle
     */
    public function setAngle($angle)
    {
        $this->angle = $angle;
        return $this;
    }

    /**
     * @return float
     */
    public function getAngle()
    {
        return $this->angle;
    }

    /**
     * @param string $fontfile
     */
    public function setFontfile($fontfile)
    {
        $this->fontfile = $fontfile;
        return $this;
    }

    /**
     * @return string
     */
    public function getFontfile()
    {
        if ($this->fontfile!==null){
            return $this->fontfile;
        }
        return ImageFunction::getEnFontPath();
    }

    /**
     * @param float $size
     */
    public function setSize($size)
    {
        $this->size = $size;
        return $this;
    }

    /**
     * @return float
     */
    public function getSize()
    {
        return $this->size;
    }

    /**
     * @param string $text
     */
    public function setText($text)
    {
        $this->text = $text;
        return $this;
    }

    /**
     * @return string
     */
    public function getText()
    {
        return $this->text;
    }

    /**
     * @param float $x
     */
    public function setX($x)
    {
        $this->x = $x;
        return $this;
    }

    /**
     * @return float
     */
    public function getX()
    {
        return $this->x;
    }

    /**
     * @param float $y
     */
    public function setY($y)
    {
        $this->y = $y;
        return $this;
    }

    /**
     * @return float
     */
    public function getY()
    {
        return $this->y;
    }

    /**
     * @param int $color_index
     */
    public function setColorIndex($color_index)
    {
        $this->color_index = $color_index;
        return $this;
    }

    /**
     * @return int
     */
    public function getColorIndex()
    {
        return $this->color_index;
    }

    public function isInRectangle(Rectangle $rect){
        $box_rectangle = $this->getFontBoxRectangle();
        $this_rectangle = ImageFunction::newRectangleOnSize(
            $this->x,
            $this->y,
            $box_rectangle->getWidth(),
            $box_rectangle->getHeight()
        );
        return $this_rectangle->isInRectangle($rect);
    }
    public function draw($image){
        $box_rectangle = $this->getFontBoxRectangle();
        imagefttext($image,
            $this->getSize(),
            $this->getAngle(),
            (int)$this->getX() - $box_rectangle->getMinX(),
            $this->getY() - $box_rectangle->getMinY(),
            $this->getColorIndex(),
            $this->getFontfile(),
            $this->getText()
        );
    }

    /**
     * create a gd2 image with those string
     * @return
     */
    public function toImage(){
        $box_rectangle = $this->getFontBoxRectangle();
        $img = imagecreatetruecolor(
            $box_rectangle->getWidth()+$this->y,
            $box_rectangle->getHeight()+$this->x
        );
        imagefill($img,0,0,imagecolorallocate($img,255,255,255));

        $this->draw($img);
        return $img;
    }

    /**
     * create a gd2 image with those string
     * @return \WideImage_Image
     */
    public function toWideImage(){
        return \WideImage::loadFromHandle($this->toImage());
    }
    protected function getFontBoxRectangle(){
        $box = imageftbbox(
            $this->getSize(),
            $this->getAngle(),
            $this->getFontfile(),
            $this->getText()
        );
        $box_left = min($box[0], $box[2], $box[4], $box[6]);
        $box_top  = min($box[1], $box[3], $box[5], $box[7]);
        $box_right =  max($box[0], $box[2], $box[4], $box[6]);
        $box_bottom =  max($box[1], $box[3], $box[5], $box[7]);
        return ImageFunction::newRectangleOnPoint($box_left,
            $box_top,
            $box_right+3,     //TODO understand mysterious constant "+3"
            $box_bottom+1); //TODO understand mysterious constant "+1"
    }
}